package com.javalong.affairs.module.main.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.javalong.affairs.R;
import com.javalong.affairs.base.ui.ImageTip;
import com.javalong.affairs.base.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by javalong on 2016/5/15.
 */
public class BottomBar extends LinearLayout implements View.OnClickListener {

    private int textSelectedColor = R.color.blue_main;
    private int textUnSelectedColor = R.color.blue_main;
    private List<ImageTip> imageTips;

    public BottomBar(Context context) {
        super(context);
        init();
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        imageTips = new ArrayList<>();
    }

    public void setTextColorResId(int textSelectedColor, int textUnSelectedColor) {
        this.textSelectedColor = textSelectedColor;
        this.textUnSelectedColor = textUnSelectedColor;
    }

    public void initWithData(int[] drawableRes, String[] tips) {
        LinearLayout.LayoutParams ll_param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);
        for (int i = 0; i < drawableRes.length; i++) {
            ImageTip imageTip = new ImageTip(getContext());
            imageTip.setImage(drawableRes[i]);
            imageTip.setImageSize(R.dimen.bottom_image_size);
            imageTip.setText(tips[i]);
            imageTip.setTextColor(textUnSelectedColor);
            imageTip.setTextSize(12);
            imageTip.setTag(i);
            addView(imageTip, ll_param);
            imageTips.add(imageTip);
            imageTip.setOnClickListener(this);
        }
        selected(0);
    }

    private void selected(int index) {
        for (int i = 0; i < imageTips.size(); i++) {
            ImageTip imageTip = ListUtils.getSafeItem(imageTips, i);
            if (imageTip != null) {
                imageTip.setImageLevel(1);
                imageTip.setTextColor(textUnSelectedColor);
            }
        }
        ImageTip imageTip = ListUtils.getSafeItem(imageTips, index);
        if (imageTip != null) {
            imageTip.setImageLevel(2);
            imageTip.setTextColor(textSelectedColor);
        }
    }

    @Override
    public void onClick(View v) {
        Integer integer = (Integer) v.getTag();
        selected(integer);
        if (onBottonClick != null) {
            onBottonClick.onBottomClick(integer);
        }
    }

    public void setRedCount(int index, int total) {
        ImageTip imageTip = ListUtils.getSafeItem(imageTips, index);
        if (imageTip != null) {
            imageTip.setRedCount(total);
        }
    }

    public interface OnBottomClick {
        void onBottomClick(int index);
    }

    private OnBottomClick onBottonClick;

    public void setOnBottonClick(OnBottomClick onBottonClick) {
        this.onBottonClick = onBottonClick;
    }


}
